package com.oa.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.system.entity.Permission;
import com.oa.system.mapper.PermissionMapper;
import com.oa.system.service.IPermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.system.vo.PermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    private final PermissionMapper permissionMapper;

    @Autowired
    public PermissionServiceImpl(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }


    @CommonService
    @Override
    public List<PermissionVO> findPermissionList(String postId) {
        List<Permission> list = null;
        if (postId != null) {
            Wrapper wrapper = new EntityWrapper();
            wrapper.where("id in (select permission_id from sys_post_permission where post_id = {0} and is_deleted = 2 and type = 1) and is_deleted = 2", postId);
            wrapper.orderBy("sort", true);
            list = permissionMapper.selectList(wrapper);
        } else {
            list = permissionMapper.findList();
        }

        List<PermissionVO> listVo = new ArrayList<>();
        list.forEach(d -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(d, permissionVO);
            listVo.add(permissionVO);
        });
        return recursionPermission(listVo, "0");
    }

    @Transactional
    @CommonService
    @Override
    public boolean deletePermission(String id) {
        Permission permission = selectById(id);
        if (permission != null && permission.getIsDeleted() == 2) {

            //删除叶子
            List<Permission> permissions = permissionMapper.findList();
            if (permissions != null && permissions.size() > 0) {
                return updateBatchById(recursionDelete(permissions, id));
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 递归获取叶子
     *
     * @param list
     * @param pid
     * @return
     */
    public List<Permission> recursionDelete(List<Permission> list, String pid) {
        // 查找所有菜单
        List<Permission> childrenList = new ArrayList<>();
        list.stream().filter(d -> Objects.equals(pid, d.getPId()))
                .collect(Collectors.toList())
                .forEach(d -> {
                    d.setIsDeleted(1);
                    childrenList.add(d);
                    childrenList.addAll(recursionDelete(list, d.getId()));
                });
        return childrenList;
    }

    /**
     * 递归获取树
     *
     * @param list
     * @param pid
     * @return
     */
    public List<PermissionVO> recursionPermission(List<PermissionVO> list, String pid) {
        // 查找所有菜单
        List<PermissionVO> childrenList = new ArrayList<>();
        list.stream()
                .filter(d -> Objects.equals(pid, d.getPId()))
                .collect(Collectors.toList())
                .forEach(d -> {
                    d.setPermissionVOList(recursionPermission(list, d.getId()));
                    childrenList.add(d);
                });
        Collections.sort(childrenList, (o1, o2) -> {
            if(o1.getSort() > o2.getSort()){
                return 1;
            }else if(o1.getSort() < o2.getSort()){
                return -1;
            }
            return 0;
        });
        return childrenList;
    }
}
